using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Test.ConsoleProgram.Algorithm.Solution
{
    [TestDescription("算法: 0001. 两数之和")]
    public class No0001_TwoSum : AbsBaseTestItem
    {
        public override void OnTest()
        {
            Assert.TestExe(TwoSum, new int[] { 2, 7, 11, 15 }, 9, new int[] { 0, 1 });
            Assert.TestExe(TwoSum, new int[] { 0, 0, 0 }, 0, new int[] { 0, 1 });
            Assert.TestExe(TwoSum, new int[] { 4, 5, 6, 5 }, 10, new int[] { 0, 2 });
        }

        public int[] TwoSum(int[] nums, int target)
        {
            Dictionary<int, int> dic = new Dictionary<int, int>();
            for (int i = 0; i < nums.Length; i++)
            {
                int value = nums[i];
                int complement = target - value;
                if (dic.ContainsKey(complement))
                {
                    return new int[] { dic[complement], i };
                }
                dic[value] = i;
            }
            throw new ArgumentException("No two sum solution");
        }

        /// <summary>
        /// 暴力破解
        /// </summary>
        public int[] TwoSum2(int[] nums, int target)
        {
            for (int i = 0; i < nums.Length; i++)
            {
                for (int j = i + 1; j < nums.Length; j++)
                {
                    if (nums[i] + nums[j] == target)
                    {
                        return new int[] { i, j };
                    }
                }
            }
            throw new ArgumentException("No two sum solution");
        }
    }
}
